<?php
class Rooms extends Controller {

	function Rooms()
	{
		parent::Controller();	
		$this->load->helper('form');
		$this->load->model('Rooms_model','Rooms');
		$this->load->model('Exposition_model', 'Exposition');
	}
	
	function index(){
		
	// TODO see if we can change the index to be like view($expo_id) expo_id being the one of the current logged in expo owner
	$this->load->view('layout/header_admin');
	$this->load->view('layout/footer');
				
	}
	
	// Load grid of rooms
	function view($expo_id)
	{
		
		$data['query_rooms'] = $this->Rooms->get_rooms($expo_id);
		$this->load->view('layout/header_admin');
		$this->load->view('edit_rooms', $data);
		$this->load->view('layout/footer');
		
	}
	
	// Edit room at position $x $y
	function edit_room($x,$y)
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
		
			$user_id = $this->tank_auth->get_user_id();
			$expo_id = $this->Exposition->get_expo_from_user($user_id);
			
			$data['query_room'] = $this->Rooms->get_room($expo_id, $x,$y);
		
			$this->load->view('layout/header_admin');
			$this->load->view('room_view',$data);
			$this->load->view('layout/footer');
		}
	}
	function edit($id){
	if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
			$data['query_room'] = $this->db->get_where('rooms', array('id'=>$id));
			
			$this->load->view('layout/header_admin');
			$this->load->view('room_view',$data);
			$this->load->view('layout/footer');
		}
		
	}
	
	// Add a room at the specified place
	function add_room($x,$y)
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
		
			$user_id = $this->tank_auth->get_user_id();
			$expo_id = $this->Exposition->get_expo_from_user($user_id);
			$this->Rooms->add_room($expo_id, $x,$y);
				
			$data['query_room'] = $this->Rooms->get_room($expo_id, $x,$y);
		
			$this->load->view('layout/header_admin');
			$this->load->view('room_view',$data);
			$this->load->view('layout/footer');
		}
	}
	
	// REMARQUE : DELETE ROOM EFFACE DE L'EXPOSITION TOUTES LES OEUVRES PRESENTES DANS LA SALLE
	function delete($id)
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
		
			$this->db->where('id', $id);
			$this->db->delete('rooms');	
			$this->db->where('room_id', $id);
			$this->db->delete('pieces');	
	
			redirect("expositions/owner_home");
		}
	}
	
	// MAJ de la BDD et retour à la page de view
	function confirm_changes()
	{
	
		$this->db->where('id',$_POST['id']);
		$this->db->update('rooms', array('name'=>$_POST['name'], 'is_private'=>$_POST['is_private'], 'image'=>$_POST['upload']));
							
		$user_id = $this->tank_auth->get_user_id();
		$expo_id = $this->Exposition->get_expo_from_user($user_id);
		redirect("rooms/view/$expo_id");

	}
}
